﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
using Auto.Data.DBUtility;//请先添加引用
using Auto.Data.BusMan.IDAL;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
namespace Auto.Data.BusMan.SQLServerDAL
{
    /// <summary>
    ///  Data Access Class BusAccidentProcess。
    /// </summary>
    public class BusAccidentProcess:IBusAccidentProcess
    {
        #region --New--
        /// <sumary> 
        /// Constructed Function 
        /// </sumary> 
        public BusAccidentProcess()
        {
        }
        /// <summary>
        /// Get Model
        /// </summary>
        /// <param name="objKey">key of Table</param>
        public Model.BusAccidentProcess GetModel(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT TOP 1 RecID, RefDocID, BusID, TrafficProcessRecord, DealDate");
            strSql.Append(" FROM Bus_BusAccidentProcess");
            strSql.Append(" WHERE RecID=@RecID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            Model.BusAccidentProcess model;
            DataRow dr = DbHelper.GetRecord(strSql.ToString(), parameters);
            if (dr != null)
            {
                model = new Model.BusAccidentProcess(dr);
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Get Model
        /// </summary>
        /// <param name="objKey">key of Table</param>
        public Model.BusAccidentProcess GetModel(string refDocID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT TOP 1 RecID, RefDocID, BusID, TrafficProcessRecord, DealDate");
            strSql.Append(" FROM Bus_BusAccidentProcess");
            strSql.Append(" WHERE RefDocID=@RefDocID");
            strSql.Append(" ORDER BY RecID DESC ");
            SqlParameter[] parameters = { 
                                    		StrHelper.MakeParam("@RefDocID",SqlDbType.VarChar,20,ParameterDirection.Input,refDocID),
                                 		};
            Model.BusAccidentProcess model;
            DataRow dr = DbHelper.GetRecord(strSql.ToString(), parameters);
            if (dr != null)
            {
                model = new Model.BusAccidentProcess(dr);
                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion --New--
        #region --Operate--
        /// <summary>
        /// Is Exists Or Not
        /// </summary>
        public bool Exists(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(1) FROM Bus_BusAccidentProcess  ");
            strSql.Append(" WHERE RecID=@RecID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            return DbHelper.Exists(strSql.ToString(), parameters);
        }

        public void Add(Model.BusAccident accidentmodel)
        {
           Model.BusAccidentProcess model=  GetModel(accidentmodel.DocID);
           if (model != null)
           {
               if (accidentmodel.TrafficProcessRecord != model.TrafficProcessRecord)
               {
                   model.RefDocID = accidentmodel.DocID;
                   model.BusID = accidentmodel.BusID;
                   model.TrafficProcessRecord = accidentmodel.TrafficProcessRecord;
                   model.DealDate = DateTime.Now;
                   Add(model);
               }
           }
           else
           {
               model = new Model.BusAccidentProcess();
               model.RefDocID = accidentmodel.DocID;
               model.BusID = accidentmodel.BusID;
               model.TrafficProcessRecord = accidentmodel.TrafficProcessRecord;
               model.DealDate = DateTime.Now;
               Add(model);
           }
        }

        #endregion --Operate--
        #region --SQL--
        /// <summary>
        /// Add One Record
        /// </summary>
        public void Add(Model.BusAccidentProcess model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO Bus_BusAccidentProcess(");
            strSql.Append("RefDocID,BusID,TrafficProcessRecord,DealDate)");
            strSql.Append(" VALUES (");
            strSql.Append("@RefDocID,@BusID,@TrafficProcessRecord,@DealDate)");
            SqlParameter[] parameters = SetParametersValue(model);
            try
            {
                object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parameters);
                if (obj == null)
                {
                    throw new Exception("Error When Insert Data Into BusAccidentProcess.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Update One Record
        /// </summary>
        public void Update(Model.BusAccidentProcess model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE Bus_BusAccidentProcess SET ");
            strSql.Append("RefDocID=@RefDocID,");
            strSql.Append("BusID=@BusID,");
            strSql.Append("TrafficProcessRecord=@TrafficProcessRecord,");
            strSql.Append("DealDate=@DealDate");
            strSql.Append(" WHERE RecID=@RecID ");
            SqlParameter[] parameters = SetParametersValue(model);
            DbHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// Delete One Record
        /// </summary>
        public void Delete(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE Bus_BusAccidentProcess ");
            strSql.Append(" WHERE RecID=@RecID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            DbHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion --SQL--
        #region --Get Data--
        /// <sumary> 
        /// Get Data By Primary Key 
        /// </sumary> 
        /// <param name="objKey">Key of Table</param> 
        /// <returns>DataTable</returns> 
        public DataTable GetDataByKeyValue(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT RecID, RefDocID, BusID, TrafficProcessRecord, DealDate");
            strSql.Append(" FROM Bus_BusAccidentProcess");
            strSql.Append(" WHERE RecID=@RecID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            return DbHelper.ExecuteQuery(strSql.ToString(), parameters).Tables[0];
        }
        /// <sumary> 
        /// Get Data List 
        /// </sumary> 
        /// <param name="strWhere">strWhere Format: Colname='?' AND ColName2='?'... </param> 
        /// <returns>DataTable</returns> 
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  RecID, RefDocID, BusID, TrafficProcessRecord, DealDate");
            strSql.Append(" FROM Bus_BusAccidentProcess ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return DbHelper.ExecuteQuery(strSql.ToString()).Tables[0];
        }
        /// <sumary> 
        /// Get TOP Rows Record 
        /// </sumary> 
        /// <param name="top">TOP Rows</param> 
        /// <param name="strWhere">strWhere Format: ColName1='?' AND ColName2='?' ..</param> 
        /// <param name="fieldOrder">Field Order By. Format: ColName1,ColName2...</param> 
        /// <returns>DataTable:TOP Rows</returns> 
        public DataTable GetList(int top, string strWhere, string fieldOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT ");
            if (top > 0)
            {
                strSql.Append(" TOP " + top.ToString());
            }
            strSql.Append(" RecID, RefDocID, BusID, TrafficProcessRecord, DealDate ");
            strSql.Append(" FROM Bus_BusAccidentProcess ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ORDER BY " + fieldOrder);
            return DbHelper.ExecuteQuery(strSql.ToString()).Tables[0];
        }
        /// <sumary> 
        /// Convert Table to Model List<Model.BusAccidentProcess> 
        /// </sumary> 
        /// <param name="dt">DataTable Of BusAccidentProcess</param> 
        /// <returns>List<Model.BusAccidentProcess></returns> 
        public List<Model.BusAccidentProcess> DataTableToList(DataTable dt)
        {
            List<Model.BusAccidentProcess> modelList = new List<Model.BusAccidentProcess>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.BusAccidentProcess model;
                for (int n = 0; n < rowsCount; n++)
                {
                    DataRow dr = dt.Rows[n];
                    model = new Model.BusAccidentProcess(dr);
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// Get Data List By Page
        /// </summary>
        public DataTable GetListByPage(int pageSize, int pageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
		   new SqlParameter("@tblName", SqlDbType.VarChar, 255),
		   new SqlParameter("@fldName", SqlDbType.VarChar, 255),
		   new SqlParameter("@PageSize", SqlDbType.Int),
		   new SqlParameter("@PageIndex", SqlDbType.Int),
		   new SqlParameter("@IsReCount", SqlDbType.Bit),
		   new SqlParameter("@OrderType", SqlDbType.Bit),
		   new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
		   };
            parameters[0].Value = "Bus_BusAccidentProcess";
            parameters[1].Value = "RecID";
            parameters[2].Value = pageSize;
            parameters[3].Value = pageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelper.ExecuteQueryProcedure("UP_GetRecordByPage", parameters, "ds").Tables[0];
        }

        /// <sumary> 
        /// Get Data List 
        /// </sumary> 
        ///<param name="refDocID">Ref DocID from BusAccident.DocID</param>
        /// <returns>DataTable</returns> 
        public DataTable GetListByRefDocID(string refDocID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  RecID, RefDocID, BusID, TrafficProcessRecord, DealDate");
            strSql.Append(" FROM Bus_BusAccidentProcess ");
            if (refDocID.Trim() != "")
            {
                strSql.Append(" WHERE RefDocID='" + refDocID + "'");
            }
            return DbHelper.ExecuteQuery(strSql.ToString()).Tables[0];
        }

        #endregion --Get Data--
        #region ----PrivateSet--
        /// <sumary> 
        /// Set Parameters Value 
        /// </sumary> 
        /// <param name="model">Model</param> 
        /// <returns>Parameters</returns> 
        private SqlParameter[] SetParametersValue(Model.BusAccidentProcess model)
        {
            #region --Init Data--
            model.DealDate = DateTime.Now;
            #endregion --Init Data--
            SqlParameter[] parameters = { 
                                    		StrHelper.MakeParam("@RefDocID",SqlDbType.VarChar,20,ParameterDirection.Input,model.RefDocID),
                                    		StrHelper.MakeParam("@BusID",SqlDbType.VarChar,20,ParameterDirection.Input,model.BusID),
                                    		StrHelper.MakeParam("@TrafficProcessRecord",SqlDbType.NVarChar,4000,ParameterDirection.Input,model.TrafficProcessRecord),
                                    		StrHelper.MakeParam("@DealDate",SqlDbType.SmallDateTime,0,ParameterDirection.Input,model.DealDate),

                                 		};
            return parameters;
        }
        /// <sumary> 
        /// Set Parameter Value of Key 
        /// </sumary> 
        /// <param name="objKey">Key Values</param> 
        /// <returns>Parameters</returns> 
        private SqlParameter[] SetParametersValue(object[] objKey)
        {
            SqlParameter[] parameters = { 
                                    		StrHelper.MakeParam("@RecID",SqlDbType.Int,0,ParameterDirection.Input,objKey[0]),

                                 		};
            return parameters;
        }
        #endregion ----PrivateSet--
    }
}
